package neu.soft.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import neu.soft.entity.OperationWarehouse;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.mapper.WarehouseMapper;
import neu.soft.query.BasePageQuery;
import neu.soft.service.WarehouseService;
import neu.soft.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    WarehouseMapper warehouseMapper;

    @Override
    public int addWarehouse(OperationWarehouse operationWarehouse) {

        int res = warehouseMapper.addWarehouse(operationWarehouse);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.DZYJ_NGDZ);
        }
        return res;
    }

    @Override
    public int updateWarehouse(OperationWarehouse operationWarehouse) {
        int check = warehouseMapper.findOne(operationWarehouse);
        if (check > 0){
            throw new AppException(AppExceptionCodeMsg.REPEAT_UPDATE);
        }
        int res = warehouseMapper.updateWarehouse(operationWarehouse);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.DZYJ_NGDZ);
        }
        return res;

    }

    @Override
    public int deleteWarehouse(Integer warehouseId) {

        int res = warehouseMapper.deleteWarehouse(warehouseId);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.NSRAD_JSNDSH);
        }
        return res;
    }

    @Override
    public List<OperationWarehouse> findAll() {
        return warehouseMapper.findAll();

    }

    @Override
    public OperationWarehouse findById(Integer warehouseId) {
        return warehouseMapper.findById(warehouseId);
    }

    @Override
    public PageBean<OperationWarehouse> findByPage(BasePageQuery query) {

        PageBean<OperationWarehouse> pageBean = new PageBean<>();
        PageHelper.startPage(query.getPageNow(), query.getPageSize());
        List<OperationWarehouse> warehouseList = warehouseMapper.findByPage(query);
        PageInfo<OperationWarehouse> pageInfo = new PageInfo<>(warehouseList);
        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());
        return pageBean;
    }


}
